home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C20 / Stlshape2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.1 KB  |  49 lines

  1. //: C20:Stlshape2.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Stlshape.cpp with the purge() function
  7. #include "../purge.h"
  8. #include <vector>
  9. #include <iostream>
  10. using namespace std;
  11.  
  12. class Shape {
  13. public:
  14.   virtual void draw() = 0;
  15.   virtual ~Shape() {};
  16. };
  17.  
  18. class Circle : public Shape {
  19. public:
  20.   void draw() { cout << "Circle::draw\n"; }
  21.   ~Circle() { cout << "~Circle\n"; }
  22. };
  23.  
  24. class Triangle : public Shape {
  25. public:
  26.   void draw() { cout << "Triangle::draw\n"; }
  27.   ~Triangle() { cout << "~Triangle\n"; }
  28. };
  29.  
  30. class Square : public Shape {
  31. public:
  32.   void draw() { cout << "Square::draw\n"; }
  33.   ~Square() { cout << "~Square\n"; }
  34. };
  35.  
  36. typedef std::vector<Shape*> Container;
  37. typedef Container::iterator Iter;
  38.  
  39. int main() {
  40.   Container shapes;
  41.   shapes.push_back(new Circle);
  42.   shapes.push_back(new Square);
  43.   shapes.push_back(new Triangle);
  44.   for(Iter i = shapes.begin();
  45.       i != shapes.end(); i++)
  46.     (*i)->draw();
  47.   purge(shapes);
  48. } ///:~
  49.